package mn.slide.dailynews.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpConnection implements Runnable
{
	public static HttpGet get;
	public static final int DID_START = 0;
	public static final int DID_ERROR = 1;
	public static final int DID_SUCCEED = 2;
	private static final int GET = 0;
	private static final int POST = 1;
	private static final int PUT = 2;
	private static final int DELETE = 3;
	private String url;
	private int method;
	private static Handler handler;
	private String data;
	public static StringBuilder buffer = null;
	private HttpClient httpClient;
	public String type;
	public static InputStream inputStream = null;

	public HttpConnection()
	{
		this(new Handler());
	}

	public HttpConnection(Handler h)
	{
		handler = h;
	}

	public void create(int method, String url, String data)
	{
		this.method = method;
		this.url = url;
		this.data = data;

		ConnectionManager.getInstance().push(this);
	}

	public void get(String url)
	{
		create(GET, url, null);
	}

	public void post(String url, String data)
	{
		create(POST, url, data);
	}

	public void put(String url, String data)
	{
		create(PUT, url, data);
	}

	public void delete(String url)
	{
		create(DELETE, url, null);
	}

	public void run()
	{
		handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
		httpClient = new DefaultHttpClient();
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60000);
		try
		{
			HttpResponse response = null;
			switch (method)
			{
				case GET:
					get = new HttpGet(url);
					response = httpClient.execute(get);
					break;
				case POST:
					HttpPost httpPost = new HttpPost(url);
					httpPost.setEntity(new StringEntity(data));
					response = httpClient.execute(httpPost);
					break;
				case PUT:
					HttpPut httpPut = new HttpPut(url);
					httpPut.setEntity(new StringEntity(data));
					response = httpClient.execute(httpPut);
					break;
				case DELETE:
					response = httpClient.execute(new HttpDelete(url));
					break;
			}
			processEntity(response.getEntity());
		} catch (Exception e)
		{
			handler.sendMessage(Message.obtain(handler, HttpConnection.DID_ERROR, e));
			e.printStackTrace();
		}
		ConnectionManager.getInstance().didComplete(this);
	}

	public void processEntity(HttpEntity entity) throws IllegalStateException, IOException
	{
		Log.d("success", "okey");
		inputStream = entity.getContent();
		Message message = Message.obtain(handler, DID_SUCCEED);
		handler.sendMessage(message);
	}

}
